<template>
  <el-button :icon="Download" circle :loading="loading" @click="handleDownload" />
</template>

<script setup>
import { tips, tipsError } from '@/utils/tips'
import { useLoadingBtn } from '@/hooks/useLoadingBtn'
import { Download } from '@element-plus/icons-vue'
import { invoke } from '@tauri-apps/api/core'
import { open } from '@tauri-apps/plugin-dialog'

const props = defineProps({
  row: {
    type: Object,
    default: () => ({})
  },
  serverConfigs: {
    type: Array,
    default: () => []
  }
})

const [handleDownload, loading] = useLoadingBtn(async () => {
  const { row, serverConfigs } = props
  const backupPath = await open({
    multiple: false,
    title: '选择存放目录',
    directory: true
  })
  const serverConfig = serverConfigs.value.find((item) => item.id === row.serve)
  await invoke('download_dir_files', {
    host: `${serverConfig.host}:${serverConfig.port}`,
    username: serverConfig.username,
    password: serverConfig.password,
    backupPath,
    remotePath: row.remotePath
  }).catch(err => {
    tipsError(err)
    return Promise.reject(err)
  })
  tips('下载完成')
})
</script>

<style scoped lang="scss"></style>